﻿///***************************************************************
/// 시  스  템 : Updms.Cfg
/// 단위시스템 : CFG
/// 계      층 : Presentation Layer
/// 프로그램명 : Cfg3010p
/// 파  일  명 : Cfg3010p.aspx.cs
/// 설      명 : DB백업파일 다운로드, 삭제
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2011-05-11 송시명   최초 작성
///***************************************************************
using System;
using System.Web.Security;
using System.Web.UI;

using SMFramework.Adapter;
using SMFramework.Data;
using SMFramework.Security;
using SMFramework.Utility;

using Base.Dac;



namespace Updms.Cfg
{
	public partial class Cfg3010p : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Cfg3010p";

		Global global = new Global();
		Crypto crypto = new Crypto();

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region CRUD (SelectProcess, GetFileListByArray, DeleteProcess)

		//검색
		private void SelectProcess()
		{
			FtpClient fc = new FtpClient(FTP_HOSTNAME, FTP_USERID, crypto.Decrypt(FTP_PASSWORD));

			try
			{
				fc.Login();
				fc.BinaryMode = true;

				string[] remoteDir = StringUtil.DelimitedListToStringArray(FTP_DIR_UPDMS_DB, "/");
				for (int i = 0; i < remoteDir.Length; i++)
				{
					fc.ChangeDir(remoteDir[i]);
				}

				string[] fileList = fc.GetFileInfo("*.zip");



				ltr_directory_info.Text = GetFileListByArray(fileList);
			}
			catch (Exception ex)
			{
				CallExceptionMsgBoxInAjax(ex);
			}
			finally
			{
				fc.Close();
			}
		}

		//파일목록생성
		private string GetFileListByArray(string[] fileList)
		{
			string link = String.Empty;

			if (fileList.Length > 1)
			{
				for (int i = 0; i < fileList.Length - 1; i++)
				{
					string [] fileInfo = fileList[i].Split('|');
					string fileName = fileInfo[0];
					string fileSize = ConversionUtil.AddComma((Convert.ToInt32(fileInfo[1]) / 1000).ToString()) + "Kb";
					string fileLink = "<a href=\"" + FTP_URL_UPDMS_DB + "/" + fileName + "\" target=\"_new\">";
					string deleteIcon = "<img src=\"../Common/Images/delete.png\" align=\"absmiddle\" class=\"hand\" onclick=\"chkDeleteFile('" + fileName + "');\" />";
					link += fileLink + fileName + " (" + fileSize + ")</a> " + deleteIcon + "<br />";
				}
			}
			else
			{
				link = "No Files";
			}

			return link;
		}

		//삭제
		private void DeleteProcess(string fileName)
		{
			FtpClient fc = new FtpClient(FTP_HOSTNAME, FTP_USERID, crypto.Decrypt(FTP_PASSWORD));

			try
			{
				fc.Login();
				fc.BinaryMode = true;

				string[] remoteDir = StringUtil.DelimitedListToStringArray(FTP_DIR_UPDMS_DB, "/");
				for (int i = 0; i < remoteDir.Length; i++)
				{
					fc.ChangeDir(remoteDir[i]);
				}

				fc.DeleteFile(fileName);

				string[] fileList = fc.GetFileInfo("*.zip");
				ltr_directory_info.Text = GetFileListByArray(fileList);	//삭제후 다시 조회
			}
			catch (Exception ex)
			{
				CallExceptionMsgBoxInAjax(ex);
			}
			finally
			{
				fc.Close();
			}
		}

		#endregion		

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_POPUP);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
				}
			}
		}

		#endregion

		#region 버튼이벤트

		//검색
		protected void ibtn_search_Click(object sender, EventArgs e)
		{
			SelectProcess();
		}

		//삭제
		protected void lbtn_delete_Click(object sender, EventArgs e)
		{
			DeleteProcess(h_file_nm.Value);
		}

		#endregion

	}
}
